<?php
/**
 * @title 148. 排序链表 - 冒泡超时
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

/**
 * Definition for a singly-linked list.
 * class ListNode {
 *     public $val = 0;
 *     public $next = null;
 *     function __construct($val = 0, $next = null) {
 *         $this->val = $val;
 *         $this->next = $next;
 *     }
 * }
 */
class Solution {

    /**
     * @param ListNode $head
     * @return ListNode
     */
    function sortList($head) {
        /**
         * @since 2023-12-29 超时案例25
         */
        $content = file_get_contents("/tmp/leetcode/148-25.txt");
        $dataArray = explode(",", $content);
        $xhprof = new Xhprof();
        $head = $xhprof->listNode($dataArray);
        
        /**
         * @since 2023-12-29 第一次获取链表长度
         */
        $length = 0;
        $node = $head;
        while($node){
            $node = $node->next;
            $length++;
        }

        /**
         * @since 2023-12-29 新建一个头节点，防止头节点位置调整
         */
        $newHead = new ListNode(0, $head);

        /**
         * @since 2023-12-29 冒泡排序
         */
        for($end=$length-1; $end>=1; $end--){
            /**
             * @since 2023-12-29 每次冒泡都从头节点开始
             */
            $node = $newHead;

            /**
             * @since 2023-12-29 每次冒泡都从头节点开始
             */
            for($j=0; $j<$end; $j++){
                /**
                 * @since 2023-12-29 如果当前节点的值大于下一个节点的值，则交换位置
                 */
                if($node->next->val > $node->next->next->val){
                    /**
                     * @since 2023-12-29 $node -> a -> b -> c
                     * @since 2023-12-29 $node -> b -> a -> c
                     */
                    $a = $node->next;
                    $b = $a->next;
                    $c = $b->next;

                    /**
                     * @since 2023-12-29 调整next指向
                     */
                    $node->next = $b;
                    $b->next = $a;
                    $a->next = $c;
                }

                /**
                 * @since 2023-12-29 链表下一个节点
                 */
                $node = $node->next;
            }
        }

        /**
         * @return
         */
        return $newHead->next;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.29_6.php
 */
$datas = [
    [4,2,1,3],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    // $result = $obj->(...$data);
    // $result = $obj->($xhprof->tree($data));
    $result = $obj->sortList($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
